package com.byit.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.constant.DefaultValue;
import com.byit.domain.SysInterfaceApp;
import com.byit.enums.PromptInfo;
import com.byit.log.ChannelType;
import com.byit.log.Log;
import com.byit.log.OperatorType;
import com.byit.msg.MessageResult;
import com.byit.service.ISysInterfaceAppService;
import com.byit.shrio.session.SessionHelper;
import com.byit.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接口app关联管理
 *
 * @author tjm
 * @date 2019年1月22日
 */
@RestController
@RequestMapping("/api/sys/interfaceApp")
public class SysInterfaceAppController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISysInterfaceAppService sysInterfaceAppService;


    /**
     * 添加
     */
    @Log(title = "接口app关联", operate = OperatorType.DATA_INSERT, channel = ChannelType.WEB_INTER)
    @PostMapping("/add")
    public Object add(HttpSession session,
                      @RequestParam("interfaceId") Long interfaceId,
                      @RequestParam("appIds") String appIds,
                      @RequestParam("encryptType") Integer encryptType) {
        try {
            Long userId = SessionHelper.getUserId(session);
            SysInterfaceApp sysInterface = new SysInterfaceApp();
            sysInterface.setInterfaceId(interfaceId);
            sysInterface.setEncryptType(encryptType);
            sysInterface.setOperationId(userId);
            sysInterface.setCreateTime(new Date());
            sysInterface.setUpdateTime(sysInterface.getCreateTime());
            List<Long> idList = StringUtil.splitLong(appIds, ",");
            for (Long id : idList) {
                //检查是否存在
                Map<String,Object> parm = new HashMap<>();
                parm.put("interface_id",interfaceId);
                parm.put("app_id",id);
                parm.put("is_del",0);
                List<SysInterfaceApp> sysInterfaceApps = sysInterfaceAppService.selectByMap(parm);
                //不存在才添加
                if (sysInterfaceApps.size() == 0){
                    sysInterface.setAppId(id);
                    sysInterfaceAppService.insert(sysInterface);
                }
            }
            return MessageResult.success();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return MessageResult.error(e);
        }
    }



    /**
     * 分页列表查询 已关联的应用
     */
    @Log(title = "接口app关联", operate = OperatorType.DATA_SELECT, channel = ChannelType.WEB_INTER)
    @GetMapping("/inList")
    public Object list(@RequestParam(value = "page", defaultValue = DefaultValue.PAGE_FIRST) int page,
                       @RequestParam(value = "limit", defaultValue = DefaultValue.PAGE_SIZE) int limit,
                       @RequestParam("interfaceId") Long interfaceId,
                       String appName,Integer encryptType) {
        Page<SysInterfaceApp> pageInfo = new Page<>(page, limit);
        Map<String,Object> map = new HashMap<>();
        map.put("interfaceId",interfaceId);
        if (StringUtil.isNotEmpty(appName)) {
            map.put("appName",appName);
        }
        if (StringUtil.isNotNull(encryptType)){
            map.put("encryptType",encryptType);
        }
        Page<JSONObject> sysInterfacePage = sysInterfaceAppService.selectPageData(pageInfo, map);
        return MessageResult.warpper(PromptInfo.SYS_SECCUSS, sysInterfacePage);
    }

    /**
     * 分页列表查询未关联的应用
     */
    @Log(title = "接口app关联", operate = OperatorType.DATA_SELECT, channel = ChannelType.WEB_INTER)
    @GetMapping("/outList")
    public Object outList(@RequestParam(value = "page", defaultValue = DefaultValue.PAGE_FIRST) int page,
                       @RequestParam(value = "limit", defaultValue = DefaultValue.PAGE_SIZE) int limit,
                       @RequestParam("interfaceId") Long interfaceId,
                       Long appId,String name,String type) {
        Page<JSONObject> pageInfo = new Page<>(page, limit);
        Map<String,Object> map = new HashMap<>();
        map.put("interfaceId",interfaceId);
        if (StringUtil.isNotNull(appId)) {
            map.put("appId",appId);
        }
        if (StringUtil.isNotNull(name)){
            map.put("name",name);
        }
        if (StringUtil.isNotNull(type)){
            map.put("type",type);
        }
        Page<JSONObject> sysInterfacePage = sysInterfaceAppService.selectPageOutData(pageInfo, map);
        return MessageResult.warpper(PromptInfo.SYS_SECCUSS, sysInterfacePage);
    }

    /**
     * 删除
     */
    @Log(title = "接口app关联", operate = OperatorType.DATA_DELETE, channel = ChannelType.WEB_INTER)
    @PostMapping("/del")
    public Object delete(HttpSession session, @RequestParam("ids") String ids) {
        Long userId = SessionHelper.getUserId(session);
        List<Long> idList = StringUtil.splitLong(ids, ",");
        for (Long id : idList) {
            SysInterfaceApp sysInterface = new SysInterfaceApp();
            sysInterface.setId(id);
            sysInterface.setIsDel(1);
            sysInterface.setUpdateTime(new Date());
            sysInterface.setOperationId(userId);
            sysInterfaceAppService.updateById(sysInterface);
        }
        return MessageResult.success();
    }

}
